package com.jwell.bigdata.ui.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import com.jwell.bigdata.R
import kotlinx.android.synthetic.main.item_notitle_dialog.view.*


/**
 * Created by 惧 on 2017/8/15.
 * 无标题栏对话框
 */

class NoTitleDialog {
    class Builder(private var mContext: Context) {
        private val isOutTouch = false

        private var message: String? = null
        private var cancelText: String? = null
        private var ensureText: String? = null
        private var mCancelOnclickListener: DialogInterface.OnClickListener? = null
        private var mEnsureOnclickListener: DialogInterface.OnClickListener? = null

        fun setMessage(msg: String): Builder {
            message = msg
            return this
        }

        fun setCancelText(str: String): Builder {
            cancelText = str
            return this
        }

        fun setEnsureText(str: String): Builder {
            ensureText = str
            return this
        }

        fun setOnCancelClickListener(listener: DialogInterface.OnClickListener?): Builder {
            mCancelOnclickListener = listener
            return this
        }

        fun setOnEnsureClickListener(listener: DialogInterface.OnClickListener?): Builder {
            mEnsureOnclickListener = listener
            return this
        }

        fun create(): Dialog {
            val dialog = Dialog(mContext, R.style.dialog)
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
            val convertView = LayoutInflater.from(mContext).inflate(R.layout.item_notitle_dialog, null)
            with(convertView) {
                if (message != null) {
                    tv_no_title.text = message
                }
                cancelText?.let {
                    bt_noTitle_cancel.text = it
                }
                ensureText?.let {
                    bt_noTitle_ensure.text = it
                }
                if (mCancelOnclickListener == null) {
                    bt_noTitle_cancel.visibility = View.GONE
                    text_1111.visibility = View.GONE
                }
                bt_noTitle_cancel.setOnClickListener {
                    mCancelOnclickListener?.onClick(dialog, DialogInterface.BUTTON_NEGATIVE)
                    dialog.dismiss()
                }
                bt_noTitle_ensure.setOnClickListener {
                    mEnsureOnclickListener?.onClick(dialog, DialogInterface.BUTTON_NEGATIVE)
                    dialog.dismiss()
                }
                dialog.setContentView(convertView)
                dialog.setCanceledOnTouchOutside(isOutTouch)
                return dialog
            }
        }
    }
}
